package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.MessageHistoryMapper;
import com.shop.mapper.MessageMapper;
import com.shop.po.Message;
import com.shop.po.MessageHistory;
import com.shop.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author banxia
 * @since 2023-02-24
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    MessageHistoryMapper messageHistoryMapper;


    @Override
    public List<Message> getMessageList(int shardIndex, int shardTotal, String messageType, int count) {
        return messageMapper.selectListByShardIndex(shardTotal,shardIndex,messageType,count);
/*        List<Message> list=new ArrayList<>();
        Message message = new Message();
        message.setId(1l);
        message.setMessageType("goodsPublic");
        list.add(message);
        return list;*/
    }

    @Override
    public Message addMessage(String messageType, String businessKey1, String businessKey2, String businessKey3) {
        Message mqMessage = new Message();
        mqMessage.setMessageType(messageType);
        mqMessage.setBusinessKey1(businessKey1);
        mqMessage.setBusinessKey2(businessKey2);
        mqMessage.setBusinessKey3(businessKey3);
        int insert = messageMapper.insert(mqMessage);
        if(insert>0){
            return mqMessage;
        }else{
            return null;
        }

    }

    @Transactional
    @Override
    public int completed(long id) {
        Message mqMessage = new Message();
        //完成任务
        mqMessage.setState("1");
        int update = messageMapper.update(mqMessage, new LambdaQueryWrapper<Message>().eq(Message::getId, id));
        if(update>0){

            mqMessage = messageMapper.selectById(id);
            //添加到历史表
            MessageHistory mqMessageHistory = new MessageHistory();
            BeanUtils.copyProperties(mqMessage,mqMessageHistory);
            messageHistoryMapper.insert(mqMessageHistory);
            //删除消息表
            messageMapper.deleteById(id);
            return 1;
        }
        return 0;

    }

    @Override
    public int completedStageOne(long id) {
        Message mqMessage = new Message();
        //完成阶段1任务
        mqMessage.setStageState1("1");
        return messageMapper.update(mqMessage,new LambdaQueryWrapper<Message>().eq(Message::getId,id));
    }

    @Override
    public int completedStageTwo(long id) {
        Message message = new Message();
        //完成阶段2任务
        message.setStageState2("1");
        return messageMapper.update(message,new LambdaQueryWrapper<Message>().eq(Message::getId,id));
    }

    @Override
    public int completedStageThree(long id) {
        Message mqMessage = new Message();
        //完成阶段3任务
        mqMessage.setStageState3("1");
        return messageMapper.update(mqMessage,new LambdaQueryWrapper<Message>().eq(Message::getId,id));
    }

    @Override
    public int completedStageFour(long id) {
        Message mqMessage = new Message();
        //完成阶段4任务
        mqMessage.setStageState4("1");
        return messageMapper.update(mqMessage,new LambdaQueryWrapper<Message>().eq(Message::getId,id));
    }

    @Override
    public int getStageOne(long id) {
        return Integer.parseInt(messageMapper.selectById(id).getStageState1());
    }

    @Override
    public int getStageTwo(long id) {
        return Integer.parseInt(messageMapper.selectById(id).getStageState2());
    }

    @Override
    public int getStageThree(long id) {
        return Integer.parseInt(messageMapper.selectById(id).getStageState3());
    }

    @Override
    public int getStageFour(long id) {
        return Integer.parseInt(messageMapper.selectById(id).getStageState4());
    }


}
